﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Newtonsoft.Json.Linq;
using Newtonsoft.Json;

namespace CarbonExchange.Bll
{
    public class Answer : ItemBase
    {
        internal Answer()
        {
        }

        public Answer(int auditId, int questionId)
        {
            AuditId = auditId;
            QuestionId = questionId;
            AnswerItems = new CarbonExchange.LinqUtilities.LazyList<AnswerItem>();
        }

        public int Id { get; internal set; }
        public int QuestionId { get; internal set; }
        public int AuditId { get; internal set; }
        internal JObject Settings { get; set; }
        public decimal Footprint { get; internal set; }

        public T GetSetting<T>(string name)
        {
            T retVal = default(T);
            if (Settings != null)
            {
                string convert = Settings.Value<string>(name);
                if (convert != null)
                {
                    retVal = JavaScriptConvert.DeserializeObject<T>(convert);
                }
            }
            return retVal;
        }

        public void SetSetting<T>(string name, T value)
        {
            if (Settings == null)
            {
                Settings = new JObject();
            }
            string converted = JavaScriptConvert.SerializeObject(value);
            var tmp = Settings.Property(name);
            if (tmp == null)// && value != null)
            {
                Settings.Add(name, new JValue(converted));
                //if (value is Array)
                //{
                //    Settings.Add(name, new JArray(value));
                //}
                //else
                //{
                //if (default(T) == null && !(value is string) && !(value is Array))
                //   Settings.Add(name, JObject.FromObject(value));
                //else
                //    Settings.Add(name, new JValue(value));
                //}
            }
            else// if(tmp != null && value != null)
            {
                //if (value is Array)
                //{
                //    tmp.Value = new JArray(value);
                //}
                //else
                //{
                //if (default(T) == null && !(value is string) && !(value is Array))
                tmp.Value = new JValue(converted);// JObject.FromObject(new Setting<T>() { Item = value });
                //else
                //    tmp.Value = new JValue(value);
            }
            /*else if (tmp != null && value == null)
            {
                Settings.Remove(name);
            }*/
        }

        public LinqUtilities.LazyList<AnswerItem> AnswerItems;
        public AnswerItem GetItemByName(string name)
        {
            if(AnswerItems == null)
            {
                AnswerItems = new CarbonExchange.LinqUtilities.LazyList<AnswerItem>();
            }
            return AnswerItems.ToList().Where(x => x.Name == name).SingleOrDefault();
        }

        public AnswerItem GetItemByPath(string path)
        {
            if (AnswerItems == null)
            {
                AnswerItems = new CarbonExchange.LinqUtilities.LazyList<AnswerItem>();
            }
            var tmp = AnswerItems.ToList().Where(x => x.Path == path).SingleOrDefault();
            if (tmp == null)
            {
                //try again with or without inital slash;
                if (path.StartsWith("/"))
                    path = path.Remove(0, 1);
                else
                    path = string.Concat("/", path);
                tmp = AnswerItems.ToList().Where(x => x.Path == path).SingleOrDefault();
            }
            return tmp;
        }

        public AnswerItem SingleItem
        {
            get
            {
                if (AnswerItems == null)
                {
                    AnswerItems = new CarbonExchange.LinqUtilities.LazyList<AnswerItem>();
                }
                var tmp = AnswerItems.FirstOrDefault();
                if (tmp == null)
                {
                    tmp = new AnswerItem();
                    tmp.Name = "Default";
                    tmp.AnswerId = this.Id;
                    AnswerItems.Add(tmp);
                }
                return tmp;
            }
        }

        public void SetSingleItem(AmeeProfileItem item)
        {
            SetItem("Default", item);
        }

        public void SetSingleItem(AmeeDataCatagory item)
        {
            SetItem("Default", item);
        }

        public AnswerItem SetItem(string name, AmeeProfileItem item)
        {
            return SetItem(name, item, 1);
        }

        public AnswerItem SetItem(string name, AmeeProfileItem item, float factor)
        {
            var tmp = GetItemByName(name);
            if (tmp == null)
            {
                tmp = new AnswerItem();
                tmp.AnswerId = this.Id;
                tmp.Name = name;
                AnswerItems.Add(tmp);
            }
            tmp.AmeeId = item.Id;
            tmp.Footprint = item.AmountPerMonth * (decimal)factor;
            tmp.Path = item.Path.Substring(0, item.Path.LastIndexOf("/"));
            if (!string.IsNullOrEmpty(tmp.Path) && !tmp.Path.StartsWith("/"))
            {
                tmp.Path = string.Concat("/", tmp.Path);
            }
            return tmp;
        }

        public AnswerItem SetItem(string name, AmeeDataCatagory item)
        {
            var tmp = GetItemByName(name);
            if (tmp == null)
            {
                tmp = new AnswerItem();
                tmp.AnswerId = this.Id;
                tmp.Name = name;
            }
            tmp.AmeeId = null;
            tmp.Footprint = null;
            tmp.Path = item.Path;
            if (!string.IsNullOrEmpty(tmp.Path) && !tmp.Path.StartsWith("/"))
            {
                tmp.Path = string.Concat("/", tmp.Path);
            }
            return tmp;
        }

        public AnswerItem SetSingleItem(AmeeDataItem item, decimal footprint)
        {
            return SetItem("Default", item, footprint);
        }

        public AnswerItem SetItem(string name, AmeeDataItem item, decimal footprint)
        {
            var tmp = GetItemByName(name);
            if (tmp == null)
            {
                tmp = new AnswerItem();
                tmp.AnswerId = this.Id;
                tmp.Name = name;
            }
            tmp.AmeeId = item.Id;
            tmp.Footprint = footprint;
            tmp.Path = item.Path;
            if (!string.IsNullOrEmpty(tmp.Path) && !tmp.Path.StartsWith("/"))
            {
                tmp.Path = string.Concat("/", tmp.Path);
            }
            return tmp;
        }
    }
}
